package com.isoft.c2team3service2.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.isoft.c2team3service2.entity.Artist;
import com.isoft.c2team3service2.entity.ArtistEnumDTO;
import com.isoft.c2team3service2.entity.UserFollowArtist;
import com.isoft.c2team3service2.mapper.ArtistMapper;
import com.isoft.c2team3service2.mapper.UserFollowArtistMapper;
import com.isoft.c2team3service2.service.ArtistService;
import com.isoft.c2team3service2.util.UserHolder;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ArtistServiceImpl extends ServiceImpl<ArtistMapper, Artist> implements ArtistService {

    @Autowired
    private ArtistMapper artistMapper;

    @Autowired
    private UserFollowArtistMapper userFollowArtistMapper;

    @Override
    public List<Artist> searchArtistsByName(String name) {
        // 若姓名为空，返回空列表（避免全表查询）
        if (name == null || name.trim().isEmpty()) {
            return List.of();
        }
        return artistMapper.selectByNameLike(name.trim());
    }

    @Override
    public List<Artist> getArtistsByUserFollows(Integer userId) {
        // 获取用户关注的艺人ID列表
        List<Integer> artistIds = userFollowArtistMapper.selectFollowedArtistIds(userId);

        // 根据艺人ID列表查询艺人信息
        if (artistIds.isEmpty()) {
            return List.of();
        }

        return artistMapper.selectBatchIds(artistIds);
    }

    @Override
    public boolean followArtist(Integer artistId) {
        // 获取当前用户ID
        Integer userId = UserHolder.getUser().getId();

        // 检查是否已经关注
        int followCount = userFollowArtistMapper.checkFollowStatus(userId, artistId);
        if (followCount > 0) {
            throw new BusinessException("您已关注该艺人"); // 抛出异常而不是返回true
        }

        // 添加关注记录
        UserFollowArtist follow = new UserFollowArtist();
        follow.setUserId(userId);
        follow.setArtistId(artistId);
        follow.setCreateTime(LocalDateTime.now());
        return userFollowArtistMapper.insert(follow) > 0;
    }

    @Override
    public boolean unfollowArtist(Integer artistId) {
        // 获取当前用户ID
        Integer userId = UserHolder.getUser().getId();

        // 删除关注记录
        LambdaQueryWrapper<UserFollowArtist> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserFollowArtist::getUserId, userId)
                .eq(UserFollowArtist::getArtistId, artistId);
        return userFollowArtistMapper.delete(wrapper) > 0;
    }

    @Override
    public boolean isFollowing(Integer artistId) {
        // 获取当前用户ID
        Integer userId = UserHolder.getUser().getId();

        // 检查关注状态
        return userFollowArtistMapper.checkFollowStatus(userId, artistId) > 0;
    }

    @Override
    public List<ArtistEnumDTO> searchArtists() {
        List<Artist> artists = artistMapper.selectIdName();

        return artists.stream().map(artist -> {
            ArtistEnumDTO artistEnumDTO = new ArtistEnumDTO();
            BeanUtils.copyProperties(artist, artistEnumDTO);
            artistEnumDTO.setName(artist.getName());
            artistEnumDTO.setId(artist.getId());
            return artistEnumDTO;
        }).toList();

    }
}